<?php
namespace app\admin\controller;

use app\common\model\GoodsOrderModel;
use web\util\OrderUtil;

/**
 * 统计
 * @author jinxiwang
 *
 */
class Stats extends Base {
    
    public function index(){
        
        $startDate = date('Ymd',strtotime(date('Y-m-d') . "-1 month"));
        $endDate = date('Ymd');
        $this->assign('startDate',$startDate);
        $this->assign('endDate',$endDate);
        return $this->fetch();
    }
    
    public function rs(){
        $result = [];
        $startDate = $this->request->param('start_date');
        $endDate = $this->request->param('end_date');
        $startDate = date('Ymd',strtotime($startDate));
        $endDate = date('Ymd',strtotime($endDate));
        
        $days = getBetweenDays($startDate, $endDate);
        $dateMap = GoodsOrderModel::instance()->where('date','between',[$startDate,$endDate])->whereIn('ispay',OrderUtil::getPayOrderStatus())->group('date')->column('date,count(0) count');
        $list = [];
        $date = $endDate;
        for($i = 0;$i <= $days;$i++){
            $date = date('Ymd',strtotime($endDate. " -$i day"));
            $list[$date] = array_key_exists($date, $dateMap) ? $dateMap[$date] : 0;
        }
        
        $result['dates'] = [];
        $result['values'] = [];
        ksort($list);
        foreach ($list as $key => $val){
            $result['dates'][]=$key;
            $result['values'][]= $val;
        }
        return json(['data'=>$result]);
        
    }
}